package com.ymm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Advice;
import com.ymm.domain.entity.Exam;
import com.ymm.domain.entity.User;
import com.ymm.domain.vo.AdviceVo;
import com.ymm.domain.vo.ExamListVo;
import com.ymm.domain.vo.PageVo;
import com.ymm.domain.vo.ResponseResult;
import com.ymm.mapper.AdviceMapper;
import com.ymm.service.AdviceService;
import com.ymm.service.ExamService;
import com.ymm.service.UserService;
import com.ymm.utils.BeanCopyUtils;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Advice)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-04-14 16:02:18
 */
@Service("adviceService")
public class AdviceServiceImpl extends ServiceImpl<AdviceMapper, Advice> implements AdviceService {

    @Resource
    private UserService userService;
    @Resource
    private AdviceService adviceService;
    @Resource
    private ExamService examService;

    @Override
    public ResponseResult getExamList(Integer pageNum, Integer pageSize, String name, Integer dataType) {
        List<Long> uIds = new ArrayList<>();
        //判断学生的姓名是否为空
        if (StringUtils.hasText(name)) {
            //姓名不为空查询姓名类似的UserId
            LambdaQueryWrapper<User> uw = new LambdaQueryWrapper<>();
            uw.like(User::getName, name);
            List<User> users = userService.list(uw);
            uIds = users.stream().map(user -> user.getId()).collect(Collectors.toList());
        }
        //添加一条数据，让uIds的size大于0
        uIds.add(-1L);
        //查询全部的体检数据
        LambdaQueryWrapper<Exam> ew = new LambdaQueryWrapper<>();
        ew.in(StringUtils.hasText(name), Exam::getUserId, uIds);
        ew.orderByDesc(Exam::getCreateBy);
        //进行连表查询，查询出体检的用户信息

        List<Long> eIds = getEIDByDataType();
        eIds.add(-1L);
        if (dataType == 1) {
            //查询未建议的体检
            ew.in(Exam::getId, eIds);
        } else if (dataType == 2) {
            //查询已添加建议的体检
            ew.notIn(Exam::getId, eIds);
        }
        Page<Exam> page = new Page<>(pageNum, pageSize);
        examService.page(page, ew);
        List<Exam> exams = page.getRecords();
        //复制成vo类型
        List<ExamListVo> examListVos = BeanCopyUtils.copyBeanList(exams, ExamListVo.class);
        //用户id列表
        List<Long> examUids = exams.stream().map(exam -> exam.getUserId()).collect(Collectors.toList());
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.in(User::getId, examUids);
        List<User> users = userService.list(userWrapper);
        examListVos = examListVos.stream().map(examListVo -> {
            User user = getUserById(users, examListVo.getUserId());
            examListVo.setName(user.getName());
            examListVo.setUserName(user.getUserName());
            examListVo.setPhoneNum(user.getPhoneNum());
            examListVo.setEmail(user.getEmail());
            examListVo.setIdCard(user.getIdCard());
            return examListVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(new PageVo(examListVos, page.getTotal()));
    }

    @Override
    public ResponseResult getAdviceByExam(Long id) {
        LambdaQueryWrapper<Advice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Advice::getExamId, id);
        List<Advice> advices = list(wrapper);
        List<AdviceVo> adviceVos = BeanCopyUtils.copyBeanList(advices, AdviceVo.class);
        List<Long> docIds = advices.stream().map(advice -> advice.getDoctorId()).collect(Collectors.toList());
        if (docIds.size() <= 0) {
            docIds.add(-1L);
        }
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.in(User::getId, docIds);
        List<User> users = userService.list(userWrapper);
        adviceVos = adviceVos.stream()
                .map(adviceVo -> adviceVo.setDoctorName(getUserById(users, adviceVo.getDoctorId()).getName()))
                .collect(Collectors.toList());
        return ResponseResult.okResult(adviceVos);
    }

    private User getUserById(List<User> users, Long userId) {
        User user = users.stream()
                .filter(u -> u.getId().longValue() == userId)
                .findFirst()
                .orElse(null);
        return user;
    }

    private List<Long> getEIDByDataType() {
        List<Advice> advices = list();
        List<Long> ids = advices.stream().map(advice -> advice.getExamId()).collect(Collectors.toList());
        return ids;
    }
}

